package com.pda.yuanjing.pages.dealerscanf;

import android.content.Context;

import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.beanin.CodeBeanIn;
import com.pda.yuanjing.beanout.BaseBeanOut;
import com.pda.yuanjing.thread.CodeBeans;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class DealerScanfModelImpl extends BaseModel implements DealerScanfModel {
    private String TAG = "InScanfModelImpl";
    private BaseBeanOut baseBeanOut;
    private BaseBeanOut baseBeanOutt;
    public DealerScanfModelImpl(Context context) {
        super(context);
    }
    public int num = 0;

    @Override
    public void addCode(String billID,String dealerId, CodeBeans codeBeans, final CallBack callBack) {
        String str = "";
        try {
            baseBeanOut = new BaseBeanOut();
            baseBeanOut.setErrcode(-1);
            baseBeanOut.setErrmsg(SetConfig.getConnectError());
            CodeBeanIn codeBeanIn = new CodeBeanIn();
            codeBeanIn.setAction("UpdateCodesDealer");
            codeBeanIn.setCid(SharedPreferencesManger.getCid());
            codeBeanIn.setUserID(SharedPreferencesManger.getUid());
            codeBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            codeBeanIn.setBillID(billID);
            codeBeanIn.setDealerID(dealerId);
            codeBeanIn.setCode(codeBeans.getCodes().get(0).getStr());
            str = codeBeans.getCodes().get(0).getStr();
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(codeBeanIn));
            final Call<ResponseBody> call = apisync.addCode(body);
            Response<ResponseBody> response = call.execute();
            if (response.isSuccessful()) {
                LogUtils.e(StringProcess.responsetoString(response.body()));
                baseBeanOut = gson.fromJson(StringProcess.responsetoString(response.body()), BaseBeanOut.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            codeBeans.removeCode(codeBeans.getCodes().get(0));
            if(baseBeanOut.getErrcode()==0){
                callBack.onupdateView(str,1);
            }else {
                callBack.onshowres(baseBeanOut.getErrmsg());
            }
        }
    }

    @Override
    public void deleteCode(String billID, CodeBeans codeBeans, final CallBack callBack) {
        String str = "";
        try {
            baseBeanOutt = new BaseBeanOut();
            baseBeanOutt.setErrcode(-1);
            baseBeanOutt.setErrmsg(SetConfig.getConnectError());
            CodeBeanIn codeBeanIn = new CodeBeanIn();
            codeBeanIn.setAction("DeleteCode");
            codeBeanIn.setCid(SharedPreferencesManger.getCid());
            codeBeanIn.setUserID(SharedPreferencesManger.getUid());
            codeBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            codeBeanIn.setBillID(billID);
            codeBeanIn.setIsVirtualTuo("0");
            codeBeanIn.setBillType("-1");
            codeBeanIn.setCode(codeBeans.getCodes().get(0).getStr());
            str = codeBeans.getCodes().get(0).getStr();
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(codeBeanIn));
            final Call<ResponseBody> call = apisync.addCode(body);
            Response<ResponseBody> response = call.execute();
            if (response.isSuccessful()) {
                LogUtils.e(StringProcess.responsetoString(response.body()));
                baseBeanOutt = gson.fromJson(StringProcess.responsetoString(response.body()), BaseBeanOut.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            codeBeans.removeCode(codeBeans.getCodes().get(0));
            if(baseBeanOutt.getErrcode()==0){
                callBack.onupdateView(str,0);
            }else {
                callBack.onshowres(baseBeanOutt.getErrmsg());
            }
        }
    }
}

